package com.dfec.dao.imp;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.alibaba.fastjson.JSON;
import com.dfec.dao.IUserDao;
import com.dfec.model.User;
import com.dfec.utils.Base64Coder;
import com.dfec.utils.DataSourceUtils;
import com.dfec.utils.DruidPlugin;
import com.dfec.utils.Token;

public class UserDaoImp implements IUserDao {
//	两种连接池切换
//	QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	@Override
	public int save(User user) throws SQLException {
			String sql ="insert into auther_user(user_name,email,phone,user_pass,state,code) values(?,?,?,?,?,?)";
			Object[] params= {user.getUser_name(),user.getEmail(),user.getPhone(),user.getUser_pass(),user.getState(),user.getCode()};
		return qr.update(sql, params);

	}

	@Override
	public int activeUser(String code) throws SQLException {
			String sql="update auther_user set state=?,code=? where code=?";
			Object[] params= {1,"",code};
		return  qr.update(sql, params);
	}

	@Override
	public int getUserByEmail(User user) throws SQLException {
		// TODO Auto-generated method stub
		String sql="select id,user_name,email,phone,state,code,qq,head_icon,token from auther_user where  email='"+user.getEmail()+"'";
		 List<User> re = qr.query(sql, new BeanListHandler<User>(User.class));
		return re.size();
	}

	@Override
	public int getUserByToken(User user) throws SQLException {
		// TODO 监察token是否有效
		String sql="select id,user_name,email,phone,state,code,qq,head_icon,token from auther_user where  token='"+user.getToken()+"'";
		 List<User> re = qr.query(sql, new BeanListHandler<User>(User.class));
		return re.size();
	}

	@Override
	public User login(User user) throws SQLException {
		// TODO Auto-generated method stub
		 String sql="select id,user_name,email,phone,state,code,qq,head_icon,token from auther_user where phone=? and user_pass=?";
		 Object[] params= {user.getPhone(),Base64Coder.encodeString(user.getUser_pass())};
		 List<User> re = qr.query(sql,params, new BeanListHandler<User>(User.class));
		 User tokenuser=null;
		 if(re.size()>0) {
			 tokenuser= creatToken(re.get(0));
			 updateUserToken(tokenuser);
		 }
		return tokenuser;
	}
	//为用户生成token
	User  creatToken(User us){
		String token=Token.makeToken(64);
		us.setToken(token);
		return us;
	}
	
	//更新用户token
	void updateUserToken(User us) {
		String sql="update auther_user set token=? where phone=? and user_pass=?";
		Object[] params= {us.getToken(),us.getPhone(),us.getUser_pass()};
	  try {
		 qr.update(sql, params);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	}

	@Override
	public User getUserByPhone(User user) throws SQLException {
		// TODO Auto-generated method stub
		 String sql="select id,user_name,email,phone,state,code,qq,head_icon,token from auther_user where phone=?";
		 Object[] params= {user.getPhone()};
		 List<User> re = qr.query(sql,params, new BeanListHandler<User>(User.class));
		return re.size()==0?null:re.get(0);
	}
	@Override
	public User getUserById(User user) throws SQLException {
		// TODO Auto-generated method stub
		

		 String sql="select id,user_name,email,phone,state,code,qq,head_icon,token from auther_user where id=?";
		 Object[] params= {user.getId()};
		 List<User> re = qr.query(sql,params, new BeanListHandler<User>(User.class));
		return re.size()==0?null:re.get(0);
	}

	@Override
	public boolean updateUserIcon(User user,String url) throws SQLException {
		// 更新用户头像
		String sql="update auther_user set head_icon=? where phone=?";
		Object[] params= {url,user.getPhone()};
		return qr.update(sql, params)>0;
	}

	@Override
	public boolean updateUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		String sql="update auther_user set ";
		String base=sql;
		if(user.getHead_icon()!=null&&user.getHead_icon().length()>0)
		sql+="head_icon='"+user.getHead_icon()+"'";
		
		if(user.getEmail()!=null&&user.getEmail().length()>0)
		sql+="email='"+user.getEmail()+"'";
		
		if(user.getPhone()!=null&&user.getPhone().length()>0)
		sql+="phone='"+user.getPhone()+"'";
		
		if(user.getUser_name()!=null&&user.getUser_name().length()>0)
		sql+="user_name='"+user.getUser_name()+"'";
		
		if(user.getUser_pass()!=null&&user.getUser_pass().length()>0)
		sql+="user_pass='"+user.getUser_pass()+"'";
		
		if(user.getQq()!=null&&user.getQq().length()>0)
			sql+="qq='"+user.getQq()+"'";
		
		if(sql.equals(base)) {
			return false;
		}else {
			sql+=" where id="+user.getId();
		}
		
		return qr.update(sql)>0;
	}
}
